//
//  UNCenterManager.swift
//  TTCOM-iOS
//
//  Created by Toj on 11/2/22.
//

import UIKit

public typealias UMNotificationPresentHandler = ((UNTriggerType, UNNotificationContent) -> (Bool))
public typealias UMNotificationResponceReceiveHandler = ((UNTriggerType, UNNotificationContent) -> (Void))

// 推送触发器类型
public enum UNTriggerType: Int {
    case push        // 推送
    case time        // 时间
    case calendar    // 日历
    case location    // 位置
    case other = 9999 // 其他
}

open class UNCenterManager: NSObject {
    public static let manager = UNCenterManager()
   
    // 收到本地/ 远程推送消息 需要展现回调
    private var willPresentResponceHandler: UMNotificationPresentHandler?
    public func setWillPresentResponceHandler(_ handler: UMNotificationPresentHandler?) {
        willPresentResponceHandler = handler
    }
    
    // 点击本地/远程推送后 收到响应回调
    private var didReceiveResponceHandler: UMNotificationResponceReceiveHandler?
    public func setDidReceiveResponceHandler(_ handler: UMNotificationResponceReceiveHandler?) {
        didReceiveResponceHandler = handler
    }
}

extension UNCenterManager: UNUserNotificationCenterDelegate {
    
    /**
     * 接收推送消息(本地推送/ 远程推送)
     * 1. 活跃状态下:
     *    本地推送/ 远程推送 逻辑处理一致
     * 2. 非活跃状态下:
     *    本地推送: 不在处理, 直接跳过
     *    远程推送: 横幅展示
     */
    public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       willPresent notification: UNNotification,
                                       withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // app活跃状态
        guard UIApplication.shared.app_isActive else {
            // 不在活跃状态 横幅显示
            var options: UNNotificationPresentationOptions = [.alert, .sound]
            if #available(iOS 14.0, *) {
                options.insert(.banner)
            }
            completionHandler(options)
            return
        }
        
        let request = notification.request
        // 解析类型
        let type = request.trigger?.to_triggerType ?? .other
        // 推送内容
        let content = request.content
        
        let isShowAlert = willPresentResponceHandler?(type, content) ?? false
        guard isShowAlert else { return }
        
        var options: UNNotificationPresentationOptions = [.alert, .sound]
        if #available(iOS 14.0, *) {
            options.insert(.banner)
        }
        completionHandler(options)
    }
    
    /** 点击响应本地/ 远程推送通知 */
    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let request = response.notification.request
        
        // 解析类型
        let type = request.trigger?.to_triggerType ?? .other
        // 推送内容
        let content = request.content
        
        didReceiveResponceHandler?(type, content)
        
        completionHandler()
    }
}

private extension UNNotificationTrigger {
    
    var to_triggerType: UNTriggerType {
        
        if self is UNPushNotificationTrigger {
            // print("-->toj: 收到本地通知")
            return .push
        }
        
        if self is UNTimeIntervalNotificationTrigger {
            // print("-->toj: 时间间隔触发器")
            return .time
        }
        
        if self is UNCalendarNotificationTrigger {
            // print("-->toj: 日历触发器")
            return .calendar
        }
        
        if self is UNLocationNotificationTrigger {
            // print("-->toj: 位置触发器")
            return .location
        }
        return .other
    }
}
